<script setup lang="ts">
import Dialog from "@/view/Resource/advertising/dialog.vue";
import useAdvertising from "@/view/Resource/advertising/utils/hook.tsx";
import { Delete, Edit, InfoFilled } from "@element-plus/icons-vue";
import { onMounted } from "vue";

const {
  dialog,
  advertisingList,
  getAdvertisingList,
  addAdvertising,
  editAdvertising,
  deleteAdvertising
} = useAdvertising()

onMounted(() => {
  getAdvertisingList()
})
</script>

<template>
  <el-card class="box-card">
    <div class="card-body-title">
      <el-button type="primary" @click="addAdvertising">添加广告</el-button>
    </div>
    <div class="card-body-table">
      <el-table
        border
        :data="advertisingList"
        class="el-table-box"
        ref="multipleTableRef"
        style="width: 100%"
      >
        <el-table-column label="#" width="60" align="center" type="index"/>
        <el-table-column prop="name" align="center" label="名称"/>
        <el-table-column prop="url" align="center" label="链接">
          <template #default="{row}">
            <p class="text">
              {{ row.url }}
            </p>
          </template>
        </el-table-column>
        <el-table-column prop="position" align="center" label="位置">
          <template #default="{row}">
            {{ row.position === 'community' ? '社区' : '课程' }}
          </template>
        </el-table-column>
        <el-table-column prop="status" align="center" label="状态">
          <template #default="{row}">
            {{ row.status ? '启用' : '禁用' }}
          </template>
        </el-table-column>
        <el-table-column align="center" label="控制" width="220">
          <template #default="{row}">
            <div class="operator-btn">
              <el-button type="primary" @click="editAdvertising(row)" size="default" :icon="Edit as any" circle/>
              <el-popconfirm
                width="200"
                @confirm="deleteAdvertising(row._id)"
                confirm-button-text="确定"
                cancel-button-text="取消"
                :icon="InfoFilled as any"
                icon-color="#f36b6c"
                title="您确定删除此广告吗?"
              >
                <template #reference>
                  <el-button type="danger" size="default" :icon="Delete as any" circle/>
                </template>
              </el-popconfirm>
            </div>
          </template>
        </el-table-column>
      </el-table>
    </div>
    <Dialog ref="dialog" @get="getAdvertisingList"/>
  </el-card>
</template>

<style scoped lang="scss">
.box-card {
  padding: 0 50px;

  .card-body-title {
    height: 35px;
    display: flex;
    justify-content: flex-end;
  }

  .card-body-table {
    margin-top: 20px;

    .el-table-box {
      height: calc(90vh - 140px);
    }
  }
}

.text {
  overflow: hidden;
  white-space: nowrap;
  text-overflow: ellipsis;
}
</style>
